#include "hal_device.h"

static hal_device* m_device_buf[HAL_DEVICE_MAX_NUM];

static uint8_t m_device_cnt = 0;


hal_device* hal_device_find(const int8_t* name)
{
    if (name == NULL)
    {
        return NULL;
    }

    for (uint32_t i = 0; i < HAL_DEVICE_MAX_NUM; i++)
    {
        if (!strncmp(name, m_device_buf[i]->name, HAL_DEVICE_NAME_MAX_LEN))
        {
            return m_device_buf[i];
        }
    }

    return NULL;
}

uint32_t hal_device_set_name(hal_device* dev, const int8_t* name)
{
    if (strlen(name) >= HAL_DEVICE_NAME_MAX_LEN)
        return HAL_ERROR_PARAM_FAILED;

    strcpy((char*)dev->name, name);
    return HAL_SUCCESS;
}


uint8_t* hal_device_get_name(hal_device* dev)
{
    return dev->name;
}


uint32_t hal_device_register(hal_device* dev, const int8_t* name)
{
    if (dev == NULL)
    {
        return HAL_ERROR_NULL;
    }

    if (hal_device_find(name) != NULL)
    {
        return HAL_ERROR_PARAM_FAILED;
    }

    if (m_device_cnt >= HAL_DEVICE_MAX_NUM)
    {
        return HAL_ERROR_NO_MEM;
    }

    m_device_buf[m_device_cnt] = dev;
    strncpy(m_device_buf[m_device_cnt]->name, name, HAL_DEVICE_NAME_MAX_LEN);
    m_device_cnt++;

    return HAL_SUCCESS;
}


uint32_t hal_device_init(hal_device* dev)
{
    uint32_t hal_ret = HAL_SUCCESS;

    if (dev == NULL || dev->init == NULL)
    {
        return HAL_ERROR_NULL;
    }

    hal_ret = dev->init(dev);

    if (hal_ret == HAL_SUCCESS)
    {
        dev->status = DEVICE_STATUS_INITIALIZED;
    }

    return hal_ret;
}

uint32_t hal_device_open(hal_device* dev, uint16_t flg)
{
    uint32_t hal_ret = HAL_SUCCESS;

    if (dev == NULL || dev->open == NULL)
    {
        return HAL_ERROR_NULL;
    }

    hal_ret = dev->open(dev, flg);

    if (hal_ret == HAL_SUCCESS)
    {
        if (dev->status != DEVICE_STATUS_OPENED)
        {
            dev->status = DEVICE_STATUS_OPENED;
        }
    }

    return hal_ret;
}

uint32_t hal_device_close(hal_device* dev)
{
    uint32_t hal_ret = HAL_SUCCESS;

    if (dev == NULL || dev->close == NULL)
    {
        return HAL_ERROR_NULL;
    }

    hal_ret = dev->close(dev);

    if (hal_ret == HAL_SUCCESS)
    {
        if (dev->status != DEVICE_STATUS_COLSED)
        {
            dev->status = DEVICE_STATUS_COLSED;
        }
    }

    return hal_ret;
}

uint32_t hal_device_start(hal_device* dev, uint16_t flg)
{
    uint32_t hal_ret = HAL_SUCCESS;

    if (dev == NULL || dev->start == NULL)
    {
        return HAL_ERROR_NULL;
    }

    hal_ret = dev->start(dev, flg);

    return hal_ret;
}

uint32_t hal_device_stop(hal_device* dev)
{
    uint32_t hal_ret = HAL_SUCCESS;

    if (dev == NULL || dev->stop == NULL)
    {
        return HAL_ERROR_NULL;
    }

    hal_ret = dev->stop(dev);

    return hal_ret;
}

uint32_t hal_device_write(hal_device* dev, uint32_t opt, uint8_t* buf, uint32_t len)
{
    if((dev == NULL) || (dev->write == NULL))
    {
        return HAL_ERROR_NULL;
    }

    return dev->write(dev, opt, buf, len);
}


uint32_t hal_device_read(hal_device* dev, uint32_t opt, uint8_t* buf, uint32_t len)
{
    if ((dev == NULL) || (dev->read) == NULL)
    {
        return HAL_ERROR_NULL;
    }

    return dev->read(dev, opt, buf, len);
}





